package com.seeyon.ctp.rest.resources;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.brwh.kit.StrKit;
import com.seeyon.apps.brwh.manager.CustomOrgManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.organization.bo.V3xOrgAccount;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.bo.V3xOrgUnit;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.organization.po.OrgMember;
import org.apache.commons.logging.Log;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.*;


@Path("brwh")
@Produces({MediaType.APPLICATION_JSON})
public class CustomOrganizationResources extends BaseResource {

    private static final Log log = CtpLogFactory.getLog(CustomOrganizationResources.class);

    private CustomOrgManager customOrgManager = (CustomOrgManager) AppContext.getBean("customOrgManager");
    private OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");

    /**
     * 获取组织架构信息接口
     *
     * @param param 请求参数，格式为Map<String, Object>
     * @return Response 返回响应对象
     * @throws Exception 异常处理
     */
    @POST
    @Path("custom/organization/datas")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response getOrganizationData(Map<String, Object> param) throws Exception {
        log.info("获取组织架构信息接口人参json：" + JSON.toJSONString(param));
        List<Map<String, Object>> unitList = new ArrayList<Map<String, Object>>();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            JSONObject jsonObj = new JSONObject(param);
            String isIncrement = StrKit.str(jsonObj.get("is_increment"));
            List<V3xOrgUnit> v3xOrgUnitList = null;
            if ("true".equals(isIncrement)) {
                v3xOrgUnitList = customOrgManager.findOrgUnitData(new Date());
            } else {
                v3xOrgUnitList = customOrgManager.findOrgUnitData(null);
            }
            for (V3xOrgUnit v3xOrgUnit : v3xOrgUnitList) {
                Map<String, Object> unitMap = new HashMap<String, Object>();
                unitMap.put("id", v3xOrgUnit.getId());
                unitMap.put("code", v3xOrgUnit.getCode());
                unitMap.put("name", v3xOrgUnit.getName());
                unitMap.put("type", v3xOrgUnit.getType());
                V3xOrgAccount v3xOrgAccount = orgManager.getAccountById(v3xOrgUnit.getOrgAccountId());
                unitMap.put("accountCode", v3xOrgAccount.getCode());
                V3xOrgUnit parentUnit = orgManager.getParentUnitById(v3xOrgUnit.getSuperior());
                if (parentUnit == null) {
                    unitMap.put("superiorCode", "");
                } else {
                    unitMap.put("superiorCode", parentUnit.getCode());
                }
                unitMap.put("enabled", v3xOrgUnit.getEnabled());
                unitMap.put("createTime", v3xOrgUnit.getCreateTime());
                unitMap.put("updateTime", v3xOrgUnit.getUpdateTime());
                unitList.add(unitMap);
            }
            resultMap.put("type", "S");
            resultMap.put("datas", unitList);
            return ok(JSON.toJSONString(resultMap));
        } catch (BusinessException e) {
            log.error("获取组织架构信息接口异常：", e);
            resultMap.put("type", "E");
            resultMap.put("datas", unitList);
            return ok(JSON.toJSONString(resultMap));
        }
    }


    /**
     * 接收POST请求，路径为"custom/memberInfo/datas"
     * 消费类型为MediaType.APPLICATION_JSON
     *
     * @param param 请求参数，类型为Map<String, Object>
     * @return 返回Response对象
     * @throws Exception
     */
    @POST
    @Path("custom/memberInfo/datas")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response getMembersData(Map<String, Object> param) throws Exception {
        log.info("获取人员信息接口人参json：" + JSON.toJSONString(param));
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Map<String, Object>> memberList = new ArrayList<Map<String, Object>>();
        try {
            JSONObject jsonObj = new JSONObject(param);
            String isIncrement = StrKit.str(jsonObj.get("is_increment"));
            List<OrgMember> orgMembers = null;
            if ("true".equals(isIncrement)) {
                orgMembers = customOrgManager.findOrgMemberData(new Date());
            } else {
                orgMembers = customOrgManager.findOrgMemberData(null);
            }

            for (OrgMember orgMember : orgMembers) {
                Map<String, Object> memberMap = new HashMap<String, Object>();
                memberMap.put("id", orgMember.getId());
                memberMap.put("code", orgMember.getCode());
                memberMap.put("name", orgMember.getName());
                V3xOrgMember memberById = orgManager.getMemberById(orgMember.getId());
                String loginName = memberById.getLoginName();
                memberMap.put("loginName", loginName);
                memberMap.put("createTime", orgMember.getCreateTime());
                memberMap.put("updateTime", orgMember.getUpdateTime());
                V3xOrgAccount v3xOrgAccount = orgManager.getAccountById(orgMember.getOrgAccountId());
                memberMap.put("accountCode", v3xOrgAccount.getCode());
                V3xOrgDepartment v3xOrgDepartment = orgManager.getDepartmentById(orgMember.getOrgDepartmentId());
                memberMap.put("departmentCode", v3xOrgDepartment.getCode());
                V3xOrgMember v3xOrgMember = orgManager.getMemberById(orgMember.getId());
                memberMap.put("enabled", v3xOrgMember.getEnabled());
                memberMap.put("telNumber", v3xOrgMember.getTelNumber());
                memberMap.put("birthday", v3xOrgMember.getBirthday());
                memberMap.put("officeNum", v3xOrgMember.getOfficeNum());
                memberMap.put("emailAddress", v3xOrgMember.getEmailAddress());
                memberMap.put("gender", v3xOrgMember.getGender());
                memberMap.put("hiredate", v3xOrgMember.getHiredate());
                memberList.add(memberMap);

            }
            resultMap.put("type", "S");
            resultMap.put("datas", memberList);
            return ok(JSON.toJSONString(resultMap));
        } catch (BusinessException e) {
            log.error("获取人员信息接口异常：", e);
            resultMap.put("type", "E");
            resultMap.put("datas", memberList);
            return ok(JSON.toJSONString(resultMap));
        }
    }


}
